﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Diagnostics;
using GeneralDFS.Common.Network;
using Microsoft.VisualBasic.Devices;
using GeneralDFS.Interface.Agent;
using GeneralDFS.BusinessLogical.Statistics;
using NIF.MobileActors.Core;
using NIF.MobileActors.Hosts;

namespace AgentTest
{
    [Serializable]
    public class HelloAgent:ActorBase<IHello>,IHello
    {
        private Queue<IPEndPoint> _hosts;
        public Queue<IPEndPoint> Hosts
        {
            get { return _hosts; }
            set { _hosts = value; }
        }

        private IPEndPoint localHost;
        public IPEndPoint LocalHost
        {
            get { return localHost; }
            set { localHost = value; }
        }
        private bool isLastHost = false;
     
        public void Excute()
        {
            var info=new ComputerInfo();
            var cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            PerformanceCounterCategory[] categories = PerformanceCounterCategory.GetCategories();
            PerformanceCounterCategory process = new PerformanceCounterCategory("Processor");
            string[] counterNames = process.GetInstanceNames();
            string[] counters;
            if (!isLastHost)
            {
                if (_hosts.Count == 0)
                {
                    Console.WriteLine("you are the last one");
                    isLastHost = true;
                    RemotingHost.DispatchActorTo(this, localHost);
                    return;
                }
                var ipEndPoint = _hosts.Dequeue();

                Print(ipEndPoint.ToString()+"Mem"+info.TotalPhysicalMemory);
                Print("\n性能名称:/n");
                for (int i = 0; i < categories.Length; i++)
                {
                    Print("名称:"+categories[i].CategoryName.ToString()+",类型:"+categories[i].CategoryType+";\n");
                }
                for (int i = 0; i < counterNames.Length; i++)
                {
                   Print(counterNames[i]); 
                }

                RemotingHost.DispatchActorTo(this, ipEndPoint);
                NameNodeStatistics statistics=new NameNodeStatistics(RemotingHost.Node);
                statistics.Excute();
            }
            else
            {
                Console.WriteLine("you have finished"+info.TotalPhysicalMemory); 
            }
        }

        protected override void Receive(Action<IHello> message)
        {
            message(this);
        }

        protected override void ResumeState(IActor actNewState)
        {
            Debug.Assert(actNewState is HelloAgent);
            var act = actNewState as HelloAgent;
            _hosts = act.Hosts;
            isLastHost = act.isLastHost;
            localHost = act.localHost;

        }

        public override void OnDispatched()
        {
            ActorProxy<IHello> proxy = RemotingHost.LocalFacade.GetActorProxy<IHello>(this.ID);
            proxy.Post(h => h.Excute());
        }

        public void Print(string message)
        {
            Console.WriteLine(message);
        }

        public PerformanceCounterCategory[] GetCategoryNameList()
        {
            PerformanceCounterCategory[] categories = PerformanceCounterCategory.GetCategories();
            return categories;
        }

    }
}
